﻿namespace ImageProcessing.UI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// The Home Screen Class header 
    /// </summary>
    public partial class IMG_HomeScreen : Form
    {
        /// <summary>
        /// original image bitmap
        /// </summary>
        private Entities.IMG_Image imgOriginal;

        /// <summary>
        /// edited image bitmap
        /// </summary>
        private Entities.IMG_Image imgEdited;

        /// <summary>
        ///  Initializes a new instance of the IMG_HomeScreen class.
        /// </summary>
        public IMG_HomeScreen()
        {
            try
            {
                this.InitializeComponent();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Loading Form");
            }
        }

        /// <summary>
        /// Resets the image.
        /// </summary>
        /// <param name="image">The image.</param>
        public void ResetImage(Entities.IMG_Image image)
        {
            try
            {
                this.imgOriginal = image;
                this.imgEdited = null;
                this.picBoxEdited.Image = null;
                if (this.imgOriginal == null)
                {
                    return;
                }

                this.picBoxOriginal.Image = this.imgOriginal.BitmapOfImage();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error loading image ");
            }
        }

        /// <summary>
        /// Open original Image
        /// </summary>
        /// <param name="sender">object sender</param>
        /// <param name="e">Event e</param>
        private void Btn_Open_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.ppm)|*.jpg; *.jpeg; *.gif; *.bmp; *.ppm";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string img_OpenedFilePath = openFileDialog1.FileName;
                    this.imgOriginal = IMG_Processor.IMG_OpenSave.IMG_Open(img_OpenedFilePath);
                    this.picBoxOriginal.Image = this.imgOriginal.BitmapOfImage();
                    this.imgEdited = null;
                    this.picBoxEdited.Image = null;
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, Can't Open Image");
            }
        }

        /// <summary>
        /// Saves edited image
        /// </summary>
        /// <param name="sender">object sender</param>
        /// <param name="e">Event e</param>
        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Error, must open an image");
                return;
            }

            try
            {
                SaveFileDialog dlg_save = new SaveFileDialog();
                dlg_save.Filter = "P3 Image|*.ppm |P6 Image|*.ppm|bmp Image|*.bmp";
                dlg_save.ShowDialog();
                int img_ExIndex = dlg_save.FilterIndex;
                if (img_ExIndex == 3)
                {
                    this.picBoxOriginal.Image.Save(dlg_save.FileName);
                    return;
                }
                else
                {
                    ImageProcessing.IMG_Processor.IMG_OpenSave.IMG_Save(this.imgOriginal, dlg_save.FileName, img_ExIndex);
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, Can't Save Image");
            }
        }

        /// <summary>
        /// performs flip operation
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void Btn_Flip_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.radioHorizontal.Checked == true)
                {
                    if (this.imgEdited != null)
                    {
                        this.imgEdited = IMG_Processor.IMG_Transformations.IMG_FlipHorizontal(this.imgEdited);
                        this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
                    }
                    else
                    {
                        this.imgEdited = IMG_Processor.IMG_Transformations.IMG_FlipHorizontal(this.imgOriginal);
                        this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
                    }
                }
                else if (this.radioVertical.Checked == true)
                {
                    if (this.imgEdited != null)
                    {
                        this.imgEdited = IMG_Processor.IMG_Transformations.IMG_FlipVertical(this.imgEdited);
                        this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
                    }
                    else
                    {
                        this.imgEdited = IMG_Processor.IMG_Transformations.IMG_FlipVertical(this.imgOriginal);
                        this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
                    }
                }
                else
                {
                    MessageBox.Show("Error: You must choose a flip direction");
                }
            }
            catch
            {
                MessageBox.Show("Error flipping the image");
            }
        }

        /// <summary>
        /// shear x clicked 
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void Btn_ShearX_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.imgEdited == null)
                {
                    this.imgEdited = IMG_Processor.IMG_Transformations.IMG_ShearX(this.imgOriginal, 1.22);
                    this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
                }
                else
                {
                    this.imgEdited = IMG_Processor.IMG_Transformations.IMG_ShearX(this.imgEdited, 1.22);
                    this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex); 
                MessageBox.Show("Error shearing the image");
            }
        }

        /// <summary>
        /// shear y clicked 
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void Btn_ShearY_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.imgEdited == null)
                {
                    this.imgEdited = IMG_Processor.IMG_Transformations.IMG_ShearY(this.imgOriginal, 1.22);
                    this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
                }
                else
                {
                    this.imgEdited = IMG_Processor.IMG_Transformations.IMG_ShearY(this.imgEdited, 1.22);
                    this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error shearing the image");
            }
        }

        /// <summary>
        /// grey scale pressed
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void GrayScaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_GrayScaleScreen img_GrayS = new IMG_GrayScaleScreen(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_GrayS.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// brightness option pressed
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void BrightnessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_BrightnessScreen img_BrightnessS = new IMG_BrightnessScreen(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_BrightnessS.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// histogram tool pressed
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void HistogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_Histogram img_HistS = new IMG_Histogram(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_HistS.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// resize recognition fn
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void BtnResize_Click(object sender, EventArgs e)
        {
            try
            {
                int img_NewWidth = int.Parse(this.txtNewWidth.Text);
                int img_NewHeight = int.Parse(this.txtNewHeight.Text);
                if (this.imgEdited == null)
                {
                    this.imgEdited = IMG_Processor.IMG_Resize.IMG_BiLinearResize(this.imgOriginal, img_NewWidth, img_NewHeight);
                }
                else
                {
                    this.imgEdited = IMG_Processor.IMG_Resize.IMG_BiLinearResize(this.imgEdited, img_NewWidth, img_NewHeight);
                }

                this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error resizing");
            }
        }

        /// <summary>
        /// contrast fn
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void ContrastToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_Contrast img_con = new IMG_Contrast(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_con.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex); 
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// gamma fn
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void GammaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_Gamma img_con = new IMG_Gamma(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_con.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the AlgebraToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void AlgebraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_AlgebraScreen img_ALG = new IMG_AlgebraScreen(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_ALG.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the CompressToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CompressToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_Quantization img_QTZ = new IMG_Quantization(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_QTZ.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the linearOperationsToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void LinearOperationsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_LinearOperations img_LIN = new IMG_LinearOperations(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_LIN.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the fourierToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FourierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_Fourier img_LIN = new IMG_Fourier(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                img_LIN.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the OrderFilterToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OrderFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_OrderFilter f = new IMG_OrderFilter(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                f.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the AddNoiseToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void AddNoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_AddNoise f = new IMG_AddNoise(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                f.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the AddPeriodicNoiseToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void AddPeriodicNoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI.IMG_AddingPeriodicNoise f = new IMG_AddingPeriodicNoise(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
            f.Show();
        }

        /// <summary>
        /// Handles the Click event of the Notch Filter.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void NotchFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ImageProcessor.NotchFilter nf = new ImageProcessor.NotchFilter(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                nf.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the BandFilterToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BandFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_BandFilter bandFilter = new IMG_BandFilter(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                bandFilter.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the morphologyToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MorphologyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_Morphology bandFilter = new IMG_Morphology(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                bandFilter.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }

        /// <summary>
        /// Handles the Click event of the matLabToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MatLabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UI.IMG_MatLab matlab = new IMG_MatLab(this.imgEdited == null ? this.imgOriginal : this.imgEdited, this);
                matlab.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error");
            }
        }
    }
}
